package com.egg.ylt.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.Utils.VerificationCodeInput;
import com.egg.ylt.pojo.LoginEntity;
import com.egg.ylt.presenter.impl.ImportCodePresenterImpl;
import com.egg.ylt.view.IImportCodeView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ToastUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_ImportCode extends BaseActivity<ImportCodePresenterImpl> implements IImportCodeView {


    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.iv_delete_top)
    ImageView ivDeleteTop;
    @BindView(R.id.tv_send_phone)
    TextView tvSendPhone;
    @BindView(R.id.verificationCodeInput)
    VerificationCodeInput verificationCodeInput;
    @BindView(R.id.tv_code_error)
    TextView tvCodeError;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.btn_next_code)
    TextView btnNextCode;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;
    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;


    private String mPhone;
    private TimeCount mTime;
    private String mCode;
    private AppSharedPreferences mSp;

    private int mType;

    @Override
    protected void getBundleExtras(Bundle extras) {
        mPhone = extras.getString("phone");
        mType = extras.getInt("type");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_import_code;
    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {

        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        //设置title
        includeTvTitle.setText("验证码");

        //初始化veiw
        tvSendPhone.setText("验证码已发送 + " + mPhone);
        //开启倒计时
        mTime = new TimeCount(60000, 1000);

        //本地sharedPreferences
        mSp = new AppSharedPreferences(mContext);

        verificationCodeInput.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
            public void onComplete(String content) {
                mCode = content;
            }
        });
        //获取验证码
        mPresenter.getCode(mPhone);
        mTime.start();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getCode() {

    }

    @Override
    public void onLoginSucceed(LoginEntity entity) {
        if (entity == null) {
            return;
        }
        if (mType == Constants.PHONE_LOGIN_TO_CODE) {
            //从手机号登陆页面过来
            tvCodeError.setVisibility(View.GONE);
            //把token存入本地
            mSp.putString(Constants.USER_TOKEN, entity.getToken());
            //用户id
            mSp.putString(Constants.USER_ID, entity.getId());
            //钱包金额
            mSp.putString(Constants.TOTAL_AMOUNT, entity.getTotalAmount());
            //公司id
            mSp.putString(Constants.COMPANY_ID, entity.getCompanyId());
            //手机号
            mSp.putString(Constants.USER_PHONE, entity.getPhone());
            //用户名
            mSp.putString(Constants.USER_NAME, entity.getName());
            //用户头像uri
            mSp.putString(Constants.HEAD_IMAGE, entity.getHeadImage());
            //用户积分
            mSp.putString(Constants.USER_FLOW, entity.getFlow());

            Constants.TOKEN = entity.getToken();
            Constants.COMPANYID = entity.getCompanyId();
            Constants.USERID = entity.getId();
            //跳转到首页
            readyGoThenKill(ACT_HomePage.class);

            // 登录成功发送首页优惠券请求
            EventBus.getDefault().post(new EventCenter(Constants.REQUEST_HOMEPAGE_COUPON));

        } else if (mType == Constants.FIND_PASSWORD_TO_CODE) {
            Bundle mBundle = new Bundle();
            mBundle.putString("phone", mPhone);
            mBundle.putInt("type", Constants.FIND_PASSWORD_TO_CODE);
            readyGo(ACT_SetNewPassword.class, mBundle);
        } else if (mType == Constants.UPDATE_PHONE_TO_CODE) {
            Bundle mBundle = new Bundle();
            mBundle.putString("phone", mPhone);
            mBundle.putInt("type", Constants.UPDATE_PHONE_TO_CODE);
            readyGo(ACT_SetNewPassword.class, mBundle);
        }
    }

    @Override
    public void onPasswrdError() {
        verificationCodeInput.setEnabled(true);
        tvCodeError.setVisibility(View.VISIBLE);
    }

    //设置发送短信倒计时
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tvSendCode.setClickable(false);
            tvSendCode.setText("重新发送验证码(" + (millisUntilFinished / 1000) + ")");
            tvSendCode.setTextColor(getResources().getColor(R.color.color_666666));
        }

        @Override
        public void onFinish() {
            tvSendCode.setText("重新发送验证码");
            tvSendCode.setClickable(true);
            tvSendCode.setTextColor(getResources().getColor(R.color.color_008ADE));
        }
    }

    @OnClick({R.id.include_iv_back, R.id.tv_send_code, R.id.btn_next_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
            case R.id.tv_send_code://重新发送验证码
                mPresenter.getCode(mPhone);
                mTime.start();
                break;
            case R.id.btn_next_code://登录
                if (StringUtil.isEmpty(mCode) || mCode.length() < 6) {
                    ToastUtil.makeText(mContext, "验证码不正确", false);
                    return;
                }
                ifTypeToActivity();
                break;
        }
    }

    //根据type区分到不同页面
    private void ifTypeToActivity() {
        //从手机号登陆页面过来
        mPresenter.setLogin(mPhone, Constants.COMPANYID, mCode);
    }

    @Override
    protected void onDestroy() {
        mTime.cancel();
        super.onDestroy();
    }
}
